﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FaultDiagnosisSystem.Controllers
{
    public class TrendController : Controller
    {
        // GET: Trend
        public ActionResult Index()
        {
            ViewBag.ComponentSelectList = BuildComponentSelectWithAll();
            ViewBag.DateSelectList = BuildDateSelect();
            ViewBag.PowerSelectList = BuildPowerSelect();

            return View();
        }

        /// <summary>
        /// 构建取值功率下拉菜单
        /// </summary>
        /// <returns></returns>
        private IList<SelectListItem> BuildPowerSelect()
        {
            var modelList = new BLL.TrendModel().GetAllList();
            return modelList.Select(
                m => new SelectListItem
                {
                    Text = m.PowerPoint.ToString(),
                    Value = m.ModelId.ToString()
                }
                ).ToList();
        }


        /// <summary>
        /// 构建部件,部件项包含全部
        /// </summary>
        /// <returns></returns>
        private IList<SelectListItem> BuildComponentSelectWithAll()
        {
            var componentList = new BLL.Component().GetListWithSensorData();
            if (componentList != null && componentList.Count != 0)
            {
                var list = componentList.Select(
                   c => new SelectListItem
                   {
                       Text = c.ComponentName,
                       Value = c.ComponentId.ToString()
                   }).ToList();
                return list;
            }
            else
            {
                return new List<SelectListItem>();
            }
        }

        /// <summary>
        /// 构建日期下拉菜单
        /// </summary>
        /// <returns></returns>
        private IList<SelectListItem> BuildDateSelect()
        {


            //var lastDayOfRealTime = new BLL.RealTimeFaultDataDaily().GetLastDay();
            //var dateSelectList = new List<SelectListItem>();
            //dateSelectList.Add(new SelectListItem()
            //{
            //    Text = lastDayOfRealTime.ToString("yyyy-MM-dd"),
            //    Value = lastDayOfRealTime.ToString("yyyy-MM-dd"),
            //    Selected = true
            //});
            //for (int i = -1; i > -7; i--)
            //{
            //    var day = lastDayOfRealTime.AddDays(i);
            //    dateSelectList.Add(new SelectListItem() { Text = day.ToString("yyyy-MM-dd"), Value = day.ToString("yyyy-MM-dd") });
            //}

            var today = DateTime.Today;

            var list = new List<SelectListItem>();
            list.Add(new SelectListItem { Text = @"最近一月", Value = today.AddMonths(-1).ToString("yyyy-MM-dd") });
            list.Add(new SelectListItem { Text = @"最近一季度", Value = today.AddMonths(-3).ToString("yyyy-MM-dd") });
            list.Add(new SelectListItem { Text = @"最近一年", Value = today.AddYears(-1).ToString("yyyy-MM-dd") });
            list.Add(new SelectListItem { Text = @"全部", Value = @"1970-01-01" });
            return list;
        }

        /// <summary>
        /// 获取趋势数据
        /// </summary>
        /// <param name="day"></param>
        /// <returns></returns>
        public JsonResult GetTrendData(DateTime day)
        {
            var list = new BLL.TrendStableValue().GetList(day);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
    }


}